package com.cnpinxi.controller.admin.product;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.ProductType;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.ProductTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/19.
 */
@RequestMapping("/productType")
@Controller
public class AdminProductTypeController extends BaseController {
    @Autowired
    ProductTypeService productTypeService;

    @RequestMapping("/getProductTypeList")
    public ModelAndView getProductTypeList(HttpServletRequest request, Integer page, Integer pageSize){
       ModelAndView m = new ModelAndView("/product/getProductTypeList.jsp");
        Map<String,String> map =this.showRequestParameters(request);
        List<ProductType> ParentType = productTypeService.getParentTypeList(map);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        PageInfo pageInfo = productTypeService.getPageInfoProductTypeList(map);
        m.addObject("ParentType",ParentType);
        m.addObject("pageInfo",pageInfo);
        m.addObject("bean",map);
       return m;
    }

    @RequestMapping("/getProductType")
    public ModelAndView getProductType(HttpServletRequest request){
        ModelAndView m = new ModelAndView("/product/getProductType.jsp");
        Map<String,String> map = this.showRequestParameters(request);
        List<ProductType> ParentType = productTypeService.getParentTypeList(map);
        ProductType productType = new ProductType();
        if(map.get("tid")!=null){
            productType = productTypeService.selectByPrimaryKey(Integer.parseInt(map.get("tid")));
        }
        m.addObject("ParentType",ParentType);
        m.addObject("productType",productType);
        return m;
    }

    @RequestMapping(value = "/getProductTypeSubmit")
    @ResponseBody
    public String getProductTypeSubmit(HttpServletRequest request,ProductType productType){
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {

            productTypeService.modifyProductType(productType);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
